﻿using System;
using System.Windows.Forms;
using DBConnection;
using System.Data.SqlClient;
using System.Data;

namespace TestProject
{
    public partial class Form1 : Form
    {
        private DBConnection.DBConnection _connection;
        protected String[] seps = { "," };

        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                _connection = new DBConnection.DBConnection(txtServer.Text, txtUser.Text, txtPass.Text, txtDB.Text);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
            if (_connection != null)
                MessageBox.Show("Connect successfully!");

            DataTable result = _connection.getDataTable("select name from sys.procedures", "result");

            cboSP.Items.Clear();
            for (int i = 0; i < result.Rows.Count; i++)
                cboSP.Items.Add(result.Rows[i][0]);
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            dgvData.Visible = true;
            groupBox1.Visible = false;
            //String[] columns = txtColumns.Text.Split(seps, StringSplitOptions.RemoveEmptyEntries);
            DataTable result = null;
            if (txtParametersArray.Text == "")
                result = _connection.sp_ExecReader("dbo." + cboSP.Text, "result");
            else
            {
                String[] parametersArr = txtParametersArray.Text.Split(seps, StringSplitOptions.RemoveEmptyEntries);
                String[] parametersValue = txtParametersValue.Text.Split(seps, StringSplitOptions.RemoveEmptyEntries);
                result = _connection.sp_ExecReader("dbo." + cboSP.Text, "result", parametersArr, parametersValue);
            }

            dgvData.DataSource = result;
        }

        private void btnCreateMsg_Click(object sender, EventArgs e)
        {
            dgvData.Visible = false;
            groupBox1.Visible = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            dgvData.Visible = true;
            groupBox1.Visible = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            dgvData.Visible = true;
            groupBox1.Visible = false;

            DataTable result = null;
            String[] parametersArr = { "msg_content", "msg_from", "msg_to" };
            String[] parametersValue = { rtxtContent.Text, txtFrom.Text, txtTo.Text };
            if (_connection.sp_ExecNonQuery("dbo.sp_createANewMessage", parametersArr, parametersValue) == 1)
            {
                MessageBox.Show("good job!");
                result = _connection.sp_ExecReader("dbo.sp_getMessage", "result");
                dgvData.DataSource = result;
            }
            else
                MessageBox.Show("try again");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            dgvData.Visible = true;
            groupBox1.Visible = false;
            //String[] columns = txtColumns.Text.Split(seps, StringSplitOptions.RemoveEmptyEntries);
            DataTable result = null;
            if (txtParametersArray.Text != "")
            {
                String[] parametersArr = txtParametersArray.Text.Split(seps, StringSplitOptions.RemoveEmptyEntries);
                String[] parametersValue = txtParametersValue.Text.Split(seps, StringSplitOptions.RemoveEmptyEntries);
                if (_connection.sp_ExecNonQuery("dbo." + cboSP.Text, parametersArr, parametersValue) == 1)
                {
                    MessageBox.Show("good job!");
                    result = _connection.sp_ExecReader("dbo.sp_getMessage", "result");
                    dgvData.DataSource = result;
                }
                else
                    MessageBox.Show("try again");
                
            }
        }
    }
}
